package com.ce.webframework.sec.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

//import com.ce.component.account.factory.ServiceFactory;
//import com.ce.component.account.model.Account;
//import com.ce.component.account.service.IAccountManager;
import com.ce.webframework.sec.dao.ISecUserinfoDao;
import com.ce.webframework.sec.model.SecMenuitemDO;
import com.ce.webframework.sec.model.SecUserinfoDO;
import com.ce.webframework.sec.service.ISecUserinfoService;
import com.ce.webframework.sec.vo.SecMenuitemVO;
import com.ce.webframework.sec.vo.SecUserinfoVO;
import com.sitechasia.webx.core.service.impl.BaseDOVOService;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;
import com.sitechasia.webx.core.exception.BizLayerException;


/**
 * 用户manager
 *
 */
public class SecUserinfoServiceImpl extends BaseDOVOService<SecUserinfoDO, SecUserinfoVO> implements ISecUserinfoService {
	private ISecUserinfoDao secUserinfoDao;

	/**
	 * 设置用户dao
	 *
	 * @param userDao
	 */
	public void setSecUserinfoDao(ISecUserinfoDao secUserinfoDao) {
		this.secUserinfoDao = secUserinfoDao;
		this.setDao(secUserinfoDao);
	}

	/**
	 * 根据用户名和密码得到用户信息
	 */
	public SecUserinfoVO getSecUser(String username, String password) {
		return getVO(this.secUserinfoDao.getSecUser(username, password));
	}

	/**
	 * 检查用户
	 *
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean checkUser(String usercode, String password){

		SecUserinfoDO secUserinfoDO = this.secUserinfoDao.getSecUser(usercode, password);
		if(secUserinfoDO != null){
			return true;
		}
		return false;
	}

	/**
	 * 得到分页用户信息
	 *
	 * @param userCode
	 * @param userName
	 * @param email
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page getSecUsers(String userCode, String userName, String email, int pageNo, int pageSize,
			String sortStr) {
		Page page = this.secUserinfoDao.getSecUsers(userCode, userName, email, pageNo, pageSize, sortStr);
		List<SecUserinfoDO> doList = page.getResult();
		page.setResult(getVOList(doList));
		return page;
	}

	public void addSecUser(SecUserinfoVO userinfoVO){
		SecUserinfoDO userinfoDO = this.getDO(userinfoVO);
		secUserinfoDao.addSecUser(userinfoDO);
	}

	public SecUserinfoVO findById(Long id){
		SecUserinfoVO userinfoVO = this.getVO(secUserinfoDao.findById(id));
		return userinfoVO;
	}

	public void updateSecUser(SecUserinfoVO userinfoVO){
		SecUserinfoDO userinfoDO = this.getDO(userinfoVO);
		secUserinfoDao.updateSecUser(userinfoDO);
	}

	public Page getUsers(String userCode, String userName, String sex, int pageNo, int pageSize,
			String sortStr){
		Page page = this.secUserinfoDao.getUsers(userCode, userName, sex, pageNo, pageSize, sortStr);
		List<SecUserinfoDO> doList = page.getResult();
		page.setResult(getVOList(doList));
		return page;
	}


	public void deleteSecUser(String[] delList){
		secUserinfoDao.deleteSecUser(delList);
	}

	public List<SecMenuitemVO> getMenuItemList(List<String> menuCodes, Long epId){

		List<SecMenuitemDO> dos = secUserinfoDao.getMenuItemList(menuCodes, epId);
		List<SecMenuitemVO> menuLvl1 = new ArrayList<SecMenuitemVO>();
		List<SecMenuitemVO> menuLvl2 = new ArrayList<SecMenuitemVO>();
		List<SecMenuitemVO> menuLvl3 = new ArrayList<SecMenuitemVO>();;
		if(dos != null){
		for(SecMenuitemDO temp : dos){
			SecMenuitemVO vo = new SecMenuitemVO();
			PopulateUtil.populate(temp, vo);
//			System.out.println("vo--->"+vo.getMenutext());
			if("1".equals(temp.getLvl())){
				menuLvl1.add(vo);
			}else if("2".equals(temp.getLvl())){
				menuLvl2.add(vo);
			}else if("3".equals(temp.getLvl())){
				menuLvl3.add(vo);
			}
		}

//		System.out.println("menuLvl1--->"+menuLvl1);

		//第一级
		Boolean isChild1 = false;
		Boolean isChild2 = false;
//		System.out.println("menuLvl2--->"+menuLvl2);
//		System.out.println("menuLvl3--->"+menuLvl3);
		for(SecMenuitemVO itemLvl1 : menuLvl1){//循环第一级
			List<SecMenuitemVO> tempLvl2 = new ArrayList<SecMenuitemVO>();


			for(SecMenuitemVO itemLvl2 : menuLvl2){//第二级

				if(itemLvl1.getMenuitemId().equals(itemLvl2.getParentId())){//判断父菜单
					tempLvl2.add(itemLvl2);
					isChild1 = true;

					List<SecMenuitemVO> tempLvl3 = new ArrayList<SecMenuitemVO>();

					for(SecMenuitemVO itemLvl3 : menuLvl3){//第二级

						if(itemLvl2.getMenuitemId().equals(itemLvl3.getParentId())){//判断父菜单
							tempLvl3.add(itemLvl3);
							isChild2 = true;
						}
					}
					if(isChild2 == true){
						itemLvl2.setChildBarId("childBar_"+itemLvl2.getMenuitemId());
						itemLvl2.setSecMenuitemVOs(tempLvl3);
					}
					isChild2 = false;
				}
			}
			if(isChild1 == true){
				itemLvl1.setChildBarId("childBar_"+itemLvl1.getMenuitemId());
				itemLvl1.setSecMenuitemVOs(tempLvl2);
			}
			isChild1 = false;
		}

	}
		return menuLvl1;
	}

//	public List<ConstDO> getConstList(){
//		return secUserinfoDao.getConstList();
//	}

//	public Map<String, Map<String, ConstDO>> getConstMap(){
//		List<ConstDO> constList = this.getConstList();
//		Map<String, Map<String, ConstDO>> constLevelMap = new LinkedHashMap<String, Map<String, ConstDO>>();
//		Map<String, ConstDO> constValueMap = new LinkedHashMap<String, ConstDO>();
////		Map<String, ConstDO> constTypeMap = new LinkedHashMap<String, ConstDO>();
//
//		List<ConstDO> typeList = new ArrayList<ConstDO>();
//		for(ConstDO temp : constList){
//			if(!BlankUtil.isBlank(temp.getType())){
//				typeList.add(temp);
//				constValueMap = new LinkedHashMap<String, ConstDO>();
//				constValueMap.put(temp.getConstValue(), temp);
//				constLevelMap.put(temp.getType(), constValueMap);
////				System.out.println(temp.getType()+ "--->"+temp.getConstValue());
//			}
//
//
//		}
//		for(ConstDO temp : constList){
//
//			String level = temp.getLevelRelation().substring(0,3);
////			System.out.println("level--->"+level);
//			for(ConstDO type : typeList){
//				if(level.equals(type.getLevelRelation().substring(0,3))){
//					Map<String, ConstDO> tempMap = constLevelMap.get(type.getType());
//					if(!tempMap.containsValue(temp)){
//						System.out.println(type.getType()+"---->"+temp.getLevelRelation()+"--->"+temp.getConstNameChs());
//						tempMap.put(temp.getConstValue(), temp);
//						break;
//					}
//
//				}
//			}
//
//
//		}
//
//		return constLevelMap;
//	}

	public SecUserinfoDO getUser(Long userId) {
		return secUserinfoDao.findById(userId);
	}

	/**
	 * 取得登陆用户
	 * @param SecUserinfoVO
	 */
	public SecUserinfoVO getLoginUser(String loginName, String password){
//		System.out.println("service loginName:" + loginName + ",password:" + password);
//		 //获得统一用户服务接口
//		IAccountManager iam = ServiceFactory.getManager();
//
//		//根据用户登录名获得帐号对象
//		Account loginAccount = iam.getAccountByLoginName(loginName,null);
//		System.out.println("loginAccount=" + loginAccount);
//
//		//判断用户登录权限
//		if (loginAccount == null) {
//
//			throw new BizLayerException("pms.login.001"); //没有此用户
//
//		} else if (!loginAccount.getPasswd().equals(password)) {
//
//			throw new BizLayerException("pms.login.002"); //密码不正确
//
//		} else {
//
//            //把帐户对象封装成应用系统的用户信息对象
//			System.out.println("loginAccount != null ,id=" + loginAccount.getId());
//			Long userId = loginAccount.getId();
//			//取得应有系统的用户信息对象
//			SecUserinfoVO loginUser = this.findById(userId);
//			System.out.println("loginUser=" + loginUser);
//			if (loginUser != null){
//				return loginUser;
//			}
//		}

		return null;

	}
}
